package com.yijianonghu.controller.wxwebsite.homepage;

import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yijianonghu.dal.model.LPCZType;
import com.yijianonghu.dal.model.ProIndexAd;
import com.yijianonghu.dal.model.SysBlog;
import com.yijianonghu.service.wxwebsite.banner.BannerService;
import com.yijianonghu.service.wxwebsite.lpcz.LPCZTypeListService;
import com.yijianonghu.service.wxwebsite.products.RecommendedProductsListService;
import com.yijianonghu.service.wxwebsite.products.service.RecommendedServicesListService;
import com.yijianonghu.service.wxwebsite.sannongdongtai.SanNongDongTaiListService;
import com.yijianonghu.util.Constants;
import com.yijianonghu.util.exception.NoResultReturnedException;
import com.yijianonghu.util.exceptionhandler.ControllerExceptionHandler;
import com.yijianonghu.util.result.MessageCode;
import com.yijianonghu.util.result.Result;

@Controller
@RequestMapping(value=Constants.WXConstants.WX_PREFIX+"homepage/", method=RequestMethod.POST)
public class HomePageController {
	
	private static final Logger logger=LoggerFactory.getLogger(HomePageController.class);
	
	@Autowired
	private BannerService bannerService;
	
	@Autowired
	private LPCZTypeListService lpczTypeListService;
	
	@Autowired
	private RecommendedProductsListService recommendedProductsListService;
	
	@Autowired
	private SanNongDongTaiListService sanNongDongTaiListService;
	
	@Autowired
	private RecommendedServicesListService recommendedServicesListService;
	
	@Autowired
	private ControllerExceptionHandler exceptionHandler;
	
	@RequestMapping(value="getBannerList")
	public @ResponseBody Result getBannerList(){
		Result result=new Result();		
		result.setSuccess(false);
		List<ProIndexAd> list=null;
		try {
			list = bannerService.getBannerList();
			if(list==null || list.isEmpty()){
				throw new NoResultReturnedException("首页轮播图列表为空！");
			}
		} catch (ClassNotFoundException | NoSuchMethodException | SecurityException | IllegalAccessException
				| IllegalArgumentException | InvocationTargetException e) {
			exceptionHandler.handleException(logger, "获取首页轮播图列表失败！", result, MessageCode.SYSTEM_BUSY, e);
		} catch (NoResultReturnedException e) {
			exceptionHandler.handleException(logger, "首页轮播图列表为空！", result, MessageCode.WX_NO_RESULT_RETURNED, e);
		}
		if(list!=null && (!list.isEmpty())){
			result.setInfor(list);
			result.setSuccess(true);
		}
		return result;
	}
	
	@RequestMapping(value="getLPCZTypes")
	public @ResponseBody Result getLPCZTypes(){
		Result result=new Result();
		result.setSuccess(false);
		List<LPCZType> list=null;
		try {
			list = lpczTypeListService.getLPCZTypeList();
			if(list==null || list.isEmpty()){
				throw new NoResultReturnedException("首页粮票超值分类列表为空！");
			}
		} catch (ClassNotFoundException | NoSuchMethodException | SecurityException | IllegalAccessException
				| IllegalArgumentException | InvocationTargetException e) {
			exceptionHandler.handleException(logger, "获取首页粮票超值分类列表失败！", result, MessageCode.SYSTEM_BUSY, e);
		} catch (NoResultReturnedException e) {
			exceptionHandler.handleException(logger, "首页粮票超值分类列表为空！", result, MessageCode.WX_NO_RESULT_RETURNED, e);
		}
		if(list!=null && (!list.isEmpty())){
			result.setInfor(list);
			result.setSuccess(true);
		}
		return result;
	}
	
	@RequestMapping(value="getRecommendedProducts")
	public @ResponseBody Result getRecommendedProducts(){
		Result result=new Result();
		result.setSuccess(false);
		List<SysBlog> list=null;
		try {
			list = recommendedProductsListService.getRecommendedProductsListForHomePage();
			if(list==null || list.isEmpty()){
				throw new NoResultReturnedException("首页推荐产品列表为空！");
			}
		} catch (ClassNotFoundException | NoSuchMethodException | SecurityException | IllegalAccessException
				| IllegalArgumentException | InvocationTargetException e) {
			exceptionHandler.handleException(logger, "获取首页推荐产品列表失败！", result, MessageCode.SYSTEM_BUSY, e);
		} catch (NoResultReturnedException e) {
			exceptionHandler.handleException(logger, "首页推荐产品列表为空！", result, MessageCode.WX_NO_RESULT_RETURNED, e);
		}
		if(list!=null && (!list.isEmpty())){
			result.setInfor(list);
			result.setSuccess(true);
		}
		return result;
	}
	
	@RequestMapping(value="getRecommendedServices")
	public @ResponseBody Result getRecommendedServices(){
		Result result=new Result();
		result.setSuccess(false);
		List<SysBlog> list=null;
		try {
			list = recommendedServicesListService.getRecommendedServicesListForHomePage();
			if(list==null || list.isEmpty()){
				throw new NoResultReturnedException("首页推荐服务列表为空！");
			}
		} catch (ClassNotFoundException | NoSuchMethodException | SecurityException | IllegalAccessException
				| IllegalArgumentException | InvocationTargetException e) {
			exceptionHandler.handleException(logger, "获取首页推荐服务列表失败！", result, MessageCode.SYSTEM_BUSY, e);
		} catch (NoResultReturnedException e) {
			exceptionHandler.handleException(logger, "首页推荐服务列表为空！", result, MessageCode.WX_NO_RESULT_RETURNED, e);
		}
		if(list!=null && (!list.isEmpty())){
			result.setInfor(list);
			result.setSuccess(true);
		}
		return result;
	}
	
	@RequestMapping(value="getSanNongTitleList")
	public @ResponseBody Result getSanNongTitleList(){
		Result result=new Result();
		result.setSuccess(false);
		List<Map<String, Object>> list=null;
		try {
			list = sanNongDongTaiListService.getSanNongDongTaiListForHomePage();
			if(list==null || list.isEmpty()){
				throw new NoResultReturnedException("首页三农动态列表为空！");
			}
		} catch (NoResultReturnedException e) {
			exceptionHandler.handleException(logger, "首页三农动态列表为空！", result, MessageCode.WX_NO_RESULT_RETURNED, e);
		} catch (Exception e){
			exceptionHandler.handleException(logger, "首页三农动态列表获取失败！", result, MessageCode.SYSTEM_BUSY, e);
		}
		if(list!=null && (!list.isEmpty())){
			result.setInfor(list);
			result.setSuccess(true);
		}
		return result;
	}

}
